/**
 * 流程渲染
 * @author kaige
 * @date 2022-7-23
 * @version 1.0.0
 **/
layui.define(['layer', 'form', 'dropdown', 'util'], function (exports) {
  var MOD_NAME = 'szflow', $ = layui.jquery, form = layui.form, dropdown = layui.dropdown, layer = layui.layer, util = layui.util;
  szflow = function () { };
  szflow.prototype.config = function () {
    return {
      render: {
        url: false,
        data: {}, //如采用url方式，则data是url的where条件，否则是数据
        contmenu: false, //右键菜单
        contevent: { //右键菜单 事件
          add: function (d) {
            layer.msg('添加事件');
          },
          edit: function (d) {
            layer.msg('编辑事件');
          },
          del: function (d) {
            layer.msg('删除事件');
          }
        },
        elem: 'flow-box', // 默认 id='flow-box'
        stepId: 'flow_step_', //步骤ID前缀
        rb_status: [
          '未到', // 0 流程已经草稿，未提交，暂时存储
          '待审', // 1 流程已经提交，等待下一步处理
          '通过', // 2 流程审批通过，等待下一步处理
          '拒绝', // 3 流程审批不通过，等待下一步处理
          '退回', // 4 流程被退回，可进行编辑再提交
          '预留', // 5
          '预留', // 6
          '预留', // 7 
          '作废', // 8 流程作废
          '已删', // 9 已删除过的，可以清理
        ],
      }
    };
  }

  szflow.prototype.render = function (e) {
    var t = this;
    e = $.extend(t.config().render, e);
    if ($('#' + e.elem).length < 1) {
      layer.msg('缺少dom');
      return false;
    }
    if (e.url) {
      $.get(e.url, e.data, function (res) {
        if (res.status) {
          if (typeof (res.data) !== 'object') {
            layer.msg('接口数据错误');
            return false;
          }
          e.getData = t.list2Tree(res.data);
          t.start(e);
        } else {
          layer.msg(res.msg);
          return false;
        }
      });
    } else {
      e.getData = t.list2Tree(e.data);
      t.start(e);
    }
  };

  szflow.prototype.list2Tree = function (list = []) {
    const data = JSON.parse(JSON.stringify(list)) // 浅拷贝不改变源数据
    const result = []
    if (!Array.isArray(data)) {
      return result
    }
    data.forEach(item => {
      delete item.children
    })
    const map = {}
    data.forEach(item => {
      map[item.id] = item
    })
    data.forEach(item => {
      const parent = map[item.pid]
      if (parent) {
        (parent.children || (parent.children = [])).push(item)
      } else {
        result.push(item)
      }
    })
    return result
  }

  szflow.prototype.start = function (e) {
    var that = this;
    var shtml = '<ol class="process_diagram"><li><div class="start" id="flow_start">提交' + '</div></li>';
    var lihtml = that.flowChild({ data: e.getData, stepId: e.stepId }, e);
    var obj = $('#' + e.elem);
    shtml += lihtml;
    shtml += '<li><div class="end" id="flow_end">结束</div></li></ol>';
    obj.empty();
    obj.append(shtml);
    if (e.contmenu) {//右键菜单
      that.add_contmenu('flow_start', e);//单个加
      that.contmenuChild(e.getData, e);//批量加
    }
  }

  szflow.prototype.flowChild = function (d, e) {
    var that = this;
    var s = '';
    $.each(d.data, function (i9, e9) {
      if (e9.children) {
        if (e9.name == 'col') {
          s += '<li><ul>' + that.flowChild({ data: e9.children, stepId: d.stepId }, e) + '</ul></li>';
        }
        if (e9.name == 'row') {
          s += '<li><ol>' + that.flowChild({ data: e9.children, stepId: d.stepId }, e) + '</ol></li>';
        }
      } else {
        s += '<li><div id="' + d.stepId + e9.id + '">' + e9.name + '';
        if (typeof (e9.step_id) === 'number' && typeof (e9.status) === 'number') {
          if (e.rb_status[e9.status]) {
            if (e9.now_audit_name && e9.now_audit_name.indexOf(',') !== -1 && e9.status_json) {//多人
              $sjson = JSON.parse(e9.status_json);
              if ($sjson) {
                var tms = [];
                $.each($sjson, function (ind, item) {
                  tms.push(item.audit_name + ':' + e.rb_status[item.status]);
                });
                s += '<span class="flow-icon">' + tms.join(",") + '</span>';
              }
            } else { //单人
              s += '<span class="flow-icon">' + e9.now_audit_name + ':' + e.rb_status[e9.status] + '</span>';
            }
          }
        }
        s += '</div></li>';
      }
    });
    return s;
  }

  szflow.prototype.contmenuChild = function (d, e) {
    var that = this;
    $.each(d, function (i8, e8) {
      if (e8.children) {
        that.contmenuChild(e8.children, e);
      } else {
        if (e8.name != 'col' && e8.name != 'row') {
          that.add_contmenu(e.stepId + e8.id, e);
        }
      }
    });
  }

  //右键菜单
  szflow.prototype.add_contmenu = function (id, e) {
    if (id == 'flow_start') {
      dropdown.render({
        elem: '#' + id
        , trigger: 'contextmenu'
        , id: id + '_cmid'
        , data: [{ title: '新增', id: 'add' }]
        , click: function (obj, othis) {
          if (obj.id === 'add') {
            e.contevent.add({ obj: obj, othis: othis });
          }
        }
      });
    } else {
      dropdown.render({
        elem: '#' + id
        , trigger: 'contextmenu'
        , id: id + '_cmid'
        , data: [
          { title: '新增', id: 'add' },
          { title: '编辑', id: 'edit' },
          { title: '删除', id: 'del' }
        ]
        , click: function (obj, othis) {
          if (obj.id === 'add') {
            e.contevent.add({ obj: obj, othis: othis });
          } else if (obj.id === 'edit') {
            e.contevent.edit({
              obj: obj,
              othis: othis,
              id: window.localStorage.getItem('now_flow_step_id')
            });
          } else if (obj.id === 'del') {
            e.contevent.del({
              obj: obj,
              othis: othis,
              id: window.localStorage.getItem('now_flow_step_id')
            });
          }
        },
        ready: function (elemPanel, elem) {
          var id = elem.selector.replace('#' + e.stepId, '');
          window.localStorage.setItem('now_flow_step_id', id);
        }
      });
    }
  }
  var szflow = new szflow();
  exports(MOD_NAME, szflow)
});